package com.whz.trycatch;

import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.Objects;

/**
 * @Author: sinosoft
 * @Description:
 * @Date: Create in 15:17 2021/4/30
 * @Modified By:
 */
@Slf4j
public class ExceptionDemo {
    public static void throwException() throws Exception {
        throw new Exception("抛出异常");
    }

    public static void handleException() throws Exception {
        Exception exceptionMsg = null;
        try {
            try {
                throwException();
            } catch (Exception e) {
                exceptionMsg = e;
            }
        } finally {
            try {
                throwException();
            } catch (Exception e) {
                if(Objects.nonNull(exceptionMsg)) {
                    exceptionMsg.addSuppressed(e);
                } else {
                    exceptionMsg = e;
                }
            }
            if(Objects.nonNull(exceptionMsg)) {
                throw exceptionMsg;
            }

        }

    }

    public static void main(String[] args) {
        try {
            ExceptionDemo.handleException();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
